package com.ruicar.afs.cloud.afscase.mq.service.impl;


import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruicar.afs.cloud.afscase.common.utils.CompareFieldsUtil;
import com.ruicar.afs.cloud.afscase.mq.entity.CaseMqCompareInfo;
import com.ruicar.afs.cloud.afscase.mq.entity.CompareEntity;
import com.ruicar.afs.cloud.afscase.mq.mapper.CaseMqCompareInfoMapper;
import com.ruicar.afs.cloud.afscase.mq.service.CaseMqCompareInfoService;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
@Data
@Slf4j
public class CaseMqCompareInfoServiceImpl extends ServiceImpl<CaseMqCompareInfoMapper, CaseMqCompareInfo> implements CaseMqCompareInfoService {
    public void saveInfo(CompareEntity compareEntity) {
        CaseMqCompareInfo caseMqCompareInfo = new CaseMqCompareInfo();
        JSONObject jsonObject = new JSONObject();

        try {
            if (compareEntity.getOriginal() != null && compareEntity.getTarget() != null) {
                Map map = CompareFieldsUtil.getModifyContent(compareEntity.getOriginal(), compareEntity.getTarget());
                if (map.size() > 0) {
                    caseMqCompareInfo.setDataId(compareEntity.getDataId());
                    caseMqCompareInfo.setAcceptId(compareEntity.getAcceptId());
                    caseMqCompareInfo.setApplyNo(compareEntity.getApplyNo());
                    caseMqCompareInfo.setContractNo(compareEntity.getContractNo());
                    caseMqCompareInfo.setEntityClass(compareEntity.getEntityClass());
                    caseMqCompareInfo.setVersion(compareEntity.getVersion());
                    caseMqCompareInfo.setDifferentData(jsonObject.toJSONString(map));
                    caseMqCompareInfo.setOriginalData(jsonObject.toJSONString(compareEntity.getOriginal()));
                    caseMqCompareInfo.setTargetData(jsonObject.toJSONString(compareEntity.getTarget()));
                    this.save(caseMqCompareInfo);
                }
            }
        } catch (Exception e) {
            log.error("{}-{}-{}-{}-CaseMqCompareInfo save failed from {}:error is {}", compareEntity.getAcceptId(), compareEntity.getApplyNo(), compareEntity.getContractNo(), compareEntity.getVersion(), compareEntity.getEntityClass(), e);
        }
    }
}
